/*
 * @Author: 梁卓
 * @Date: 2022-02-01 20:11:14
 * @LastEditTime: 2022-02-11 22:01:49
 * @LastEditors: Please set LastEditors
 * @Description: 格式化菜单
 * @FilePath: \dreamUi\src\utils\intiMenus.js
 */
import Home from '@/views/Home';


/**
 *  格式化菜单， 将菜单转换为组件
 * @param {Array}} menus 
 */
export const formatMenus = (menus) => {
  // 迭代菜单，并对菜单进行改造
  const route = menus.filter(item => {
    item.component = (item.component === 'home') ? Home : loadView(item.component);
    item.icon = item.icon;
    // 线路由的信息中设置值（mate 或 matched)
    item.meta = {
      title: item.title
    }
    // 处理子菜单
    if (item.children && item.children.length > 0) {
      formatMenus(item.children);
    }
    return true;
  });
  return route;
}

/**
 *  路由懒加载
 * @param {String} view  组件路径
 * @returns 路由的组件
 */
const loadView = (view) => {
  return (resolve) => require([`@/views/${view}.vue`],resolve);
}